﻿using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Client.Framework.Service
{
    /// <summary>
    /// Author: Leil
    /// Description: HttpClient客户端请求封装
    /// CreateTime: 2020/5/18 11:20:50
    /// </summary>
    class HttpService
    {
        private static HttpService instance;

        private static readonly object obj = new object();

        private readonly HttpClient client;

        private HttpService()
        {
            client = new HttpClient();
        }

        public static HttpService Instance()
        {
            if (instance == null)
            {
                lock(obj)
                {
                    if (instance == null)
                    {
                        instance = new HttpService();
                    }
                }
            }
            return instance;
        }


        /// <summary>
        /// http GET请求封装
        /// </summary>
        /// <param name="url">请求url</param>
        /// <param name="action">返回结果回调</param>
        public async void HttpGet(string url, Action<string> action)
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(url);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                action(responseBody);
            }
            catch (HttpRequestException e)
            {

                Console.WriteLine("Message :{0} ", e.Message);
            }
            
        }

        /// <summary>
        /// http POST请求封装
        /// </summary>
        /// <param name="url">请求url</param>
        /// <param name="data">请求参数</param>
        /// <param name="action">返回结果处理回调</param>
        public async void HttpPost(string url, object data, Action<string> action)
        {
            try
            {
                string json = JsonConvert.SerializeObject(data);
                StringContent content = new StringContent(json);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = await client.PostAsync(url, content);
                string responseBody = await response.Content.ReadAsStringAsync();
                action(responseBody);
            }
            catch (HttpRequestException e)
            {

                Console.WriteLine("Message :{0} ", e.Message);
            }
        }

        /// <summary>
        /// http 文件上传封装
        /// </summary>
        /// <param name="url">请求url</param>
        /// <param name="stream">文件流</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="action">返回结果回调</param>
        public async void HttpPostFile(string url, FileStream stream, string fileName, Action<string> action)
        {
            try
            {
                MultipartFormDataContent content = new MultipartFormDataContent
                {
                    { new StreamContent(stream), "file", fileName }
                };
                HttpResponseMessage response = await client.PostAsync(url, content);
                string responseBody = await response.Content.ReadAsStringAsync();
                action(responseBody);
            }
            catch (Exception e)
            {
                Console.WriteLine("Message :{0} ", e.Message);
            }

            
        }








    }
}
